//
//  ByPersonalSettingVC.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/18.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByPersonalSettingVC.h"

@interface ByPersonalSettingVC () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *table;               // 表视图
@property (nonatomic, strong) UIButton *logoutButton;           // 登出按钮

@property (nonatomic, strong) NSArray *subjects;

@end

@implementation ByPersonalSettingVC

#pragma mark - getter
- (NSArray *)subjects {
    if (!_subjects) {
        _subjects = @[@"关于学院", @"用户协议"];
    }
    return _subjects;
}


#pragma mark - viewcontroller method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createLeftBackBarButtonItem];
    
    [self createTableView];
    
    if (BY_USER) {
        [self createLogoutView];
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    /** 设置导航栏为半透明 状态栏为白色 */
    [self setNaviAttributeWithTranslucent:NO stateBarLightContent:YES];
    
    ByTabBarController *tabBar = (ByTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    [tabBar hideTabBar];
}



#pragma mark - create view
- (void)createLogoutView {
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    bottomView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:bottomView];
    bottomView.bottom = SCREEN_HEIGHT - 64;
    
    /** 线 */
    [BoyeViewHandle lineCreatedInView:bottomView
                                 fram:CGRectMake(0, 0, bottomView.width, 1)
                                color:[UIColor colorWithWhite:0.95 alpha:1]];
    
    /** 按钮 */
    [BoyeViewHandle createButtonInView:bottomView config:^(UIButton *button) {
        button.frame = CGRectMake(SCREEN_WIDTH * 0.1, 5, SCREEN_WIDTH * 0.8, 30);
        button.backgroundColor = JM_THEME_COLOR;
        button.titleLabel.font = FONT(16);
        [button setTitle:@"退出登录" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(logoutClick) forControlEvents:UIControlEventTouchUpInside];
        button.layer.cornerRadius = 4;
    }];
    
}
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.scrollEnabled = NO;
    [self.view addSubview:self.table];
}


#pragma mark - UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.subjects.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.font = FONT(14);
    }
    
    cell.textLabel.text = self.subjects[indexPath.section];
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.5;
    } else {
        return 5;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
  
    /** 关于学院 */
    if (indexPath.section == 0) {
        ByWebViewController *vc = [[ByWebViewController alloc] initWithUrlString:@"Webview/Mix/about"];
        vc.navigationItem.title = @"关于学院";
        [self.navigationController pushViewController:vc animated:YES];
    }
    /** 用户协议 */
    else if (indexPath.section == 1) {
        ByWebViewController *vc = [[ByWebViewController alloc] initWithUrlString:@"Webview/Mix/user_agreement"];
        vc.navigationItem.title = @"用户协议";
        [self.navigationController pushViewController:vc animated:YES];
    }
}




#pragma mark - 点击事件
- (void)logoutClick {
    [ByCommonCache setUserInfo:nil];    // 退出登录
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    NSLog(@"%@ memory warning", [self class]);
}

@end
